/**
 * Jaxson ajax
 *
 * Copyright Remedy Interactive, licensed under Apache version 2 license:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0.html
 *
 * This notice must remain in all java source files.
 */
package org.jaxson.struts.reverseAjax;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.http.HttpSession;

/**
 * Manager for pushing events to the client via Reverse Ajax.
 * 
 * @author Joe Maisel
 */
public class ReverseAjaxManager
{
	public static final String BATCHED_SESSION_KEY = ReverseAjaxManager.class.getName() + ".batched";
	public static final String IMMEDIATE_SESSION_KEY = ReverseAjaxManager.class.getName() + ".immediate";
	private static final Log lager = LogFactory.getLog( ReverseAjaxManager.class.getName() );

	/**
	 * Add an event to a batch. The batch won't be sent until it's committed.
	 */
	public static void addToBatch( HttpSession session, ReverseAjaxEvent event )
	{
		if( session.getAttribute( BATCHED_SESSION_KEY ) == null )
		{
			session.setAttribute( BATCHED_SESSION_KEY, new ReverseAjaxBatch() );
		}

		ReverseAjaxBatch batch = (ReverseAjaxBatch) session.getAttribute( BATCHED_SESSION_KEY );
		batch.addEvent( event );
	}

	/**
	 * Clear a batch.
	 */
	public static void clearBatch( HttpSession session )
	{
		session.setAttribute( BATCHED_SESSION_KEY, new ReverseAjaxBatch() );
	}

	/**
	 * Commit a batch.
	 */
	public static void commitBatch( HttpSession session )
	{
		ReverseAjaxBatch batch = (ReverseAjaxBatch) session.getAttribute( BATCHED_SESSION_KEY );
		batch.setCommitted( true );
	}

	/**
	 * Send as soon as possible. In the case of streaming reverse ajax this will
	 * be within 1 second.
	 */
	public static void sendImmediately( HttpSession session, ReverseAjaxEvent evt )
	{
		if( session.getAttribute( "reverse.ajax.method" ) == null )
			return;

		lager.debug( "sendImmediately " + session.getId() );

		if( session.getAttribute( IMMEDIATE_SESSION_KEY ) == null )
		{
			ReverseAjaxBatch b = new ReverseAjaxBatch();
			session.setAttribute( IMMEDIATE_SESSION_KEY, b );
		}

		if( session.getAttribute( "reverse.ajax.method" ).equals( "stream" ) )
		{

			// TimerTask tt = new TimerTask(){
			class MyTT extends TimerTask
			{
				HttpSession session;
				ReverseAjaxEvent evt;

				MyTT (HttpSession session, ReverseAjaxEvent evt)
				{
					this.session = session;
					this.evt = evt;
				}

				@Override
				public void run()
				{
					ReverseAjaxBatch batch = (ReverseAjaxBatch) session.getAttribute( IMMEDIATE_SESSION_KEY );
					batch.addEvent( evt );
					batch.setCommitted( true );
				}
			}
			;

			MyTT tt = new MyTT( session, evt );
			Date now = new Date( System.currentTimeMillis() + 1000 );
			Timer t = new Timer();
			t.schedule( tt, now );
		}

		if( session.getAttribute( "reverse.ajax.method" ).equals( "poll" ) )
		{
			ReverseAjaxBatch batch = (ReverseAjaxBatch) session.getAttribute( IMMEDIATE_SESSION_KEY );
			batch.addEvent( evt );
			batch.setCommitted( true );
		}

	}
}
